package com.example.getfromservice.Activity;


import java.util.ArrayList;
import java.util.List;
import com.example.getfromservice.R;
import com.example.getfromservice.Book.Book;
import com.example.getfromservice.Book.BookDAO;
import com.example.getfromservice.Book.DataHandlerBook;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class FifthActivity extends Activity {

	
	private Book book;
	private Bundle extras;
	private int[] info_from_main_activity ;
	private BookDAO bookdatasource;;
	
	//*********************************************** ONCREATE ****************************************	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
       
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fifth);
        
        bookdatasource = new BookDAO(this);
        bookdatasource.open();
        
        extras = getIntent().getExtras();
        info_from_main_activity = extras.getIntArray("Info");
        

       if( info_from_main_activity[1] != 0 )
        {
    	   	// Info - { category_id, book_id , action }
        	inputdata( info_from_main_activity[0] , info_from_main_activity[1] );      
        	    
        }
    }

    
  //*********************************************** MENU ****************************************
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_fifth, menu);
        return true;
    }
    
  //*********************************************** ONPAUSE ****************************************
    
    @Override
    protected void onPause() {
      bookdatasource.close();
      super.onPause();
    }
    
  //*********************************************** ONRESUME ****************************************
  
    @Override
    protected void onResume() {
      bookdatasource.open();
      super.onResume();
    }
    
  //*********************************************** ONCLICK ****************************************
    
    public void onClick( View view)
    {
    	switch(view.getId())
	    {
	    
			case R.id.bokf : 
								{
									finish();
								}
			break;
	    }
    }
    
  //*********************************************** ISONLINE ****************************************  
    
    public boolean isOnline() {
       
    	ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
       
        if (netInfo != null && netInfo.isConnectedOrConnecting()) {
           
        	return true;
        }
        
        return false;
    }

  //*********************************************** INPUTDATA ****************************************
    
    // Info - { category_id, book_id , action }
    private void inputdata(int catId ,int bookId) {
		
    	TextView bookisbn = (TextView) findViewById(R.id.tVIsbnf);
    	TextView booktitle = (TextView) findViewById(R.id.tVTitlef);
    	TextView bookauthor = (TextView) findViewById(R.id.tVAuthorf);
    	TextView bookid = (TextView) findViewById(R.id.tVIdf);
    	ImageView image = (ImageView) findViewById(R.id.imageView1f);
    	book = new Book();	
    	
    	if( !isOnline() ) 
    		{
		    		Book tmp = bookdatasource.selectBookfromDB(bookId);
		   		 
					bookisbn.setText(tmp.getISBN());
					booktitle.setText(tmp.getTitle());
					bookauthor.setText(tmp.getAuthor());
					bookid.setText(tmp.getID()+"");
					
					
		
		
					if(tmp.getImage() != null)
						{
							image.setImageBitmap(tmp.getImage());
						}
					else
						{
								Log.i("BMP","NULL");
						}
    		}
    	else if ( isOnline() )
    		{
    		

		    		List<Book> books_list = new ArrayList<Book>();  
		    		
		    		DataHandlerBook xmltest = new DataHandlerBook();
		
		    		books_list = xmltest.getAllBooks(catId+"");
			    	  
		
					for (Book bok : books_list) {
						
						if( bok.getID() == bookId )
							{
								book = bok;
								break;
							}
					}
		
					bookisbn.setText(book.getISBN());
					booktitle.setText("\""+book.getTitle()+"\"");
					bookauthor.setText(book.getAuthor());
					bookid.setText(book.getID()+"");
					
		
					if(book.getImage() != null)
						{
							image.setImageBitmap(book.getImage());
						}
	
    	}
		
	}
 
}
